package com.huarui.user.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.ewaf.framework.cookie.CookieManager;

import com.huarui.user.model.CookieConstants;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 * @author uniseraph
 * @deprecated
 */
public class LoginInterceptor implements Interceptor {
   
	/**
	 * 
	 */
	private static final long serialVersionUID = 7701277859715822695L;
	private Logger logger = Logger.getLogger(LoginInterceptor.class);
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = (HttpServletRequest) invocation
		.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);

		HttpServletResponse response = (HttpServletResponse) invocation
		.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);

		try{
			CookieManager cookie = new CookieManager(request);
			
			String logined = cookie.getValue(CookieConstants.COOKIE_PERM,
			CookieConstants.LOGINED);
			
		    boolean isLogined = Boolean.valueOf(logined);
			
			if(isLogined){
	            	
				return invocation.invoke();
			}else{
				
	//			 response.sendRedirect("/user/login_input.do");
	//			 return null;
				return "login";
			}
		}
		catch (Exception e) {
			logger.error("判断用户登录状态时出错！", e);
			
			throw new RuntimeException(e);
		}
	}
		
}
